package com.zhuliwei.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhuliwei.constants.SystemConstants;
import com.zhuliwei.domain.ResponseResult;
import com.zhuliwei.domain.entity.Article;
import com.zhuliwei.service.ArticleService;
import com.zhuliwei.utils.RedisCache;
import com.zhuliwei.utils.SecurityUtils;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * 文章接口控制层
 *
 * @author 朱力维
 * @time 2022/10/7 10:52
 **/
@RestController
@RequestMapping("/article")
@Api(tags = "文章", description = "文章相关接口")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @Resource
    private RedisCache redisCache;

    /**
     * 获取浏览量最高的前5篇文章
     *
     * @return
     */
    @GetMapping("/hotArticleList")
    public ResponseResult hotArticleList() {
        return articleService.hotArticleList();
    }

    /**
     * 文章搜索
     * http://localhost:2001/article/search/s
     *
     * @return
     */
    @GetMapping("/search/{keyword}")
    public ResponseResult search(@PathVariable String keyword) {
        return articleService.search(keyword);
    }

    /**
     * 获取最新的5篇文章
     *
     * @return
     */
    @GetMapping("/newArticleList")
    public ResponseResult newArticleList() {
        return articleService.newArticleList();
    }

    /**
     * 查询文章数据
     */
    @GetMapping("/numData")
    public ResponseResult getArticleData() {
        return articleService.getArticleData();
    }

    /**
     * 按条件查询查询文章
     *
     * @return
     */
    @GetMapping("/articleList")
    public ResponseResult articleList(Integer pageNum,Integer pageSize,String categoryId,String paramsId) {
        return articleService.articleList(pageNum, pageSize, categoryId, paramsId);
    }

    /**
     * 更新浏览量
     */
    @PutMapping("/updateViewCount/{id}")
    public ResponseResult updateViewCount(@PathVariable String id) {
        return articleService.updateViewCount(id);
    }

    /**
     * 查询文章详情
     *
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult getArticleDetail(@PathVariable String id) {
        return articleService.getArticleDetail(id);
    }

    /**
     * 查询所有置顶文章
     *
     * @return
     */
    @GetMapping("/topList")
    public ResponseResult getTopList() {
        LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Article::getIsTop, SystemConstants.ARTICLE_IS_TOP);
        return ResponseResult.okResult(articleService.list(lqw));
    }

    /**
     * 点赞文章
     */
    @PutMapping("/like")
    public ResponseResult like(String articleId) {
        Set<String> set = redisCache.getCacheMapValue(SystemConstants.LIKES_MAP_PREFIX, articleId);
        if (CollectionUtils.isEmpty(set)) {
            set = new HashSet<>();
        }
        set.add(SecurityUtils.getUserId());
        redisCache.setCacheMapValue(SystemConstants.LIKES_MAP_PREFIX, articleId, set);
        return ResponseResult.okResult();
    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int count = 0;
        int x = in.nextInt();
        int y = in.nextInt();
        int n = in.nextInt();

        ArrayList<Integer> arr = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            arr.add(in.nextInt());
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr.get(j) > arr.get(i)) {
                    int temp = arr.get(i);
                    arr.set(i, j);
                    arr.set(j, temp);
                }
            }
        }
        for (int i = 0; i < n; i++) {
            if (x >= y) {
                return;
            }
            x = x * arr.get(i);
            for (int j = i+1; j < n; j++) {
                if (arr.get(j) == arr.get(i)) {
                    arr.remove(j);
                    n--;
                }
            }
            count++;
        }

        System.out.println(count);
    }

    /**
     * 点赞文章
     */
    @PutMapping("/like/{articleId}")
    public ResponseResult wxlike(@PathVariable String articleId) {
        Set<String> set = redisCache.getCacheMapValue(SystemConstants.LIKES_MAP_PREFIX, articleId);
        if (CollectionUtils.isEmpty(set)) {
            set = new HashSet<>();
        }
        set.add(SecurityUtils.getUserId());
        redisCache.setCacheMapValue(SystemConstants.LIKES_MAP_PREFIX, articleId, set);
        return ResponseResult.okResult();
    }

    /**
     * 取消点赞
     */
    @PutMapping("/cancelLike")
    public ResponseResult cancelLike(String articleId) {
        Set<String> set = redisCache.getCacheMapValue(SystemConstants.LIKES_MAP_PREFIX, articleId);
        set.remove(SecurityUtils.getUserId());
        redisCache.setCacheMapValue(SystemConstants.LIKES_MAP_PREFIX, articleId, set);
        return ResponseResult.okResult();
    }

    /**
     * 取消点赞
     */
    @PutMapping("/cancelLike/{articleId}")
    public ResponseResult wxCancelLike(@PathVariable String articleId) {
        Set<String> set = redisCache.getCacheMapValue(SystemConstants.LIKES_MAP_PREFIX, articleId);
        set.remove(SecurityUtils.getUserId());
        redisCache.setCacheMapValue(SystemConstants.LIKES_MAP_PREFIX, articleId, set);
        return ResponseResult.okResult();
    }

    /**
     * 获取点赞过的文章
     */
    @GetMapping("/getMyLikes")
    public ResponseResult getMyLikes() {
        return articleService.getMyLikes();
    }
}
